package com.cloudcc.mobile.view.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dao.impl.CreateNewScheduleEngineImpl;
import com.cloudcc.mobile.dialog.DatetimePickDialog;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.weight.SlideSwitch;
/**
 * 日历管理主页之新建日程
 * @author lhy
 * @date 2015-11-23 上午9:44 
 */
public class CreateNewScheduleActivty extends BaseActivity implements OnClickListener{
	private CreateNewScheduleEngineImpl engine;
	private LayoutInflater inflater = null;
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final int ADDMICROPOST_ACTIVITY_AT=8001;
	@Bind(R.id.bar_back)
	TextView cancel;
	@Bind(R.id.bar_button_plus)
	TextView save;
	@Bind(R.id.bar_title_text)
	TextView title;
	@Bind(R.id.write_schedule_name)//日程名称
	EditText schedule_name;
	@Bind(R.id.schedule_whole_day)//全天
	SlideSwitch whole_day;
	@Bind(R.id.schedule_starttime_lay)//开始时间
	LinearLayout starttimelay;
	@Bind(R.id.schedule_start_time)
	TextView start_time;
	@Bind(R.id.schedule_endtime_lay)//结束时间
	LinearLayout endtime_lay;
	@Bind(R.id.schedule_endtime)
	TextView endtime;
	@Bind(R.id.schedule_private)//私密
	SlideSwitch schedule_private;
	@Bind(R.id.add_schedule)//添加日程
	LinearLayout add_schedule;
	@Bind(R.id.show_schedule_detail)//日程详细
	LinearLayout schedule_detail;
	@Bind(R.id.schedule_participate_man_lay)//参与人
	LinearLayout participate_man_lay;
	@Bind(R.id.schedule_participate_man)//参与人
	TextView participate_man;
	@Bind(R.id.associated_business_lay)//关联企业
	LinearLayout business_lay;
	@Bind(R.id.associated_business)
	TextView business;
	@Bind(R.id.schedule_set_repeat_lay)//设置重复
	LinearLayout set_repeat_lay;
	@Bind(R.id.schedule_set_repeat)
	TextView set_repeat;
	@Bind(R.id.schedule_warn_lay)//提醒时间
	LinearLayout warn_lay;
	@Bind(R.id.schedule_warn)
	TextView warn;
	@Bind(R.id.schedule_note)//备注
	EditText note_lay;
	private AlertDialog dialog;
	int point = 3;
	int repeatpoint;
	private int heith = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		title.setText(R.string.create_new_schedule);
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		heith = size.y*3/4;
		inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		cancel.setOnClickListener(this);
		save.setOnClickListener(this);
		whole_day.setSelected(false);
		whole_day.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				/*if(whole_day.isSelected()){
					date = formatter.format(new Date());
					start_time.setText(date+getWeek(week, formatter, date));
					endtime.setText(formatter1.format(new Date()));
				}else{
					start_time.setText(formatter1.format(new Date()));
					endtime.setText(formatter1.format(new Date()));
				}*/
			}
		});
		schedule_private.setSelected(false);
		add_schedule.setOnClickListener(this);
		warn_lay.setOnClickListener(this);
		starttimelay.setOnClickListener(this);
		endtime_lay.setOnClickListener(this);
		participate_man_lay.setOnClickListener(this);
		set_repeat_lay.setOnClickListener(this);
		start_time.setText(formatter1.format(new Date()));
		endtime.setText(formatter1.format(new Date()));
		initdata();
	}
	public void initdata(){
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.create_schedule_layout;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bar_back:
			this.finish();
			break;
		case R.id.bar_button_plus:
			showWaitDialog();
			JSONObject json = new JSONObject();
			engine.SaveNewScheduleRequest(json, new ResultCallBack<Object>() {
				@Override
				public void onSuccess(Object t) {
					Toast.makeText(CreateNewScheduleActivty.this, "保存成功，请继续操作！", Toast.LENGTH_SHORT).show();
					CreateNewScheduleActivty.this.finish();
				}
				
				@Override
				public void onFailure(ErrorInfo error) {
					// TODO Auto-generated method stub
					Toast.makeText(CreateNewScheduleActivty.this, "保存失败，请重试！", Toast.LENGTH_SHORT).show();
					dismissWainting();
				}
			});
			
			break;
		case R.id.add_schedule:
			schedule_detail.setVisibility(View.VISIBLE);
			add_schedule.setVisibility(View.GONE);
			break;
		case R.id.schedule_warn_lay:
			final String descArr[] = getResources().getStringArray(R.array.array_time_warn);
			dialog = new AlertDialog.Builder(CreateNewScheduleActivty.this)
			.setTitle("请选择提醒时间").setSingleChoiceItems(descArr, point, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					warn.setText(descArr[arg1]);
					point = arg1;
					dialog.dismiss();
				}
			}).show();
			dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, heith);
			break;
		case R.id.schedule_starttime_lay:
			dialog = new DatetimePickDialog(CreateNewScheduleActivty.this, "请选择时间日期", start_time, dialog, inflater)
			.ShowDatetimeDialog();
			dialog.show();
			break;
		case R.id.schedule_endtime_lay:
			dialog = new DatetimePickDialog(CreateNewScheduleActivty.this, "请选择时间日期", endtime, dialog, inflater)
					.ShowDatetimeDialog();
			dialog.show();
			break;
		case R.id.schedule_participate_man_lay:
			Intent intent1=new Intent(CreateNewScheduleActivty.this,ChoiceContactActivity.class);
			startActivityForResult(intent1, ADDMICROPOST_ACTIVITY_AT);
			break;
		case R.id.schedule_set_repeat_lay:
			final String reoeatArr[] = getResources().getStringArray(R.array.array_set_repeat);
			dialog = new AlertDialog.Builder(CreateNewScheduleActivty.this)
			.setTitle("设置重复").setSingleChoiceItems(reoeatArr, repeatpoint, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					set_repeat.setText(reoeatArr[arg1]);
					repeatpoint = arg1;
					dialog.dismiss();
				}
			}).show();
			dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			break;
		default:
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==ADDMICROPOST_ACTIVITY_AT&&resultCode==RESULT_OK){//@某人
			String a=data.getStringExtra("name");
			String aid = data.getStringExtra("id");
			String [] c=a.split(",");
			participate_man.setText(a);
			participate_man.setTag(aid);
			System.out.println("a="+a+",aid="+aid);
			/*for(int i=0;i<c.length;i++){
				if(!c[i].equals("") && i<c.length-1){
					participate_man.append(c[i]+",");
				}
			}*/
		}
	}
}
